import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    { path: '/', redirect: '/login'},
    { path: '/login', name: 'Login', component: () => import('./components/Login.vue') },
    { path: '/layout',name:'Layout', component: () => import('./components/layout/index.vue'),
      children: [
        { path: 'select', name: 'Select', component: () => import ('./components/Select.vue') },
        { path: 'pay', name: 'Pay', component: () => import ('./components/Pay.vue') },
        { path: 'paid', name: 'Paid', component: () => import ('./components/Paid.vue') },
        { path: 'myTicket', name: 'MyTicket', component: () => import ('./components/MyTicket.vue') },
        { path: 'passenger', name: 'Passenger', component: () => import ('./components/Passenger.vue') },
        { path: 'student', name: 'Student', component: () => import ('./components/Student.vue') },
        { path: 'myInformation', name: 'MyInformation', component: () => import ('./components/MyInformation.vue') },
      ]
    } 
  ]
})

router.beforeEach((to, from, next) => {
  if(to.name === 'Login' )
    return next()
  const tokenStr = localStorage.getItem('token')
  if(!tokenStr||tokenStr === '')
    return next('/login')
  return next()
})

  
  

export default router
